import paramiko

ip_list = ["192.168.2.130", "192.168.2.131"]

for host in ip_list:
    print(host)
    # host = '192.168.1.28'
    port = 12800
    username = 'app'
    password = 'XXXXXXXXXXXX'
    # command = "echo 'ip:'`/usr/sbin/ip a|grep global|grep inet|awk '{print $2}'|awk -F'/' '{print $1}'`"
    command = "echo '服务器可以正常连接'"

    ssh = paramiko.SSHClient()
    try:
        ssh.set_missing_host_key_policy(
            paramiko.AutoAddPolicy())  # 允许连接不在know_hosts文件中的主机
        # ssh.connect(host, port, username, password, timeout=4)
        ssh.connect(host,
                    port,
                    username,
                    password,
                    timeout=4,
                    allow_agent=False,
                    look_for_keys=False)
    except:
        print('Error:', host, "连接异常")
    else:
        stdin, stdout, stderr = ssh.exec_command(command)
        # print stdout.read()    #这句和下面那两句貌似是等价的
        for std in stdout.readlines():
            print(std)

        ssh.close()